//
//  SearchByIDViewController.m
//  MooreDoll
//
//  Created by 郭景豪 on 2014/12/15.
//  Copyright (c) 2014年 Tank. All rights reserved.
//

#import "SearchByIDViewController.h"
#import "TelephonyInfo.h"
#import "ValidateText.h"
#import "AddFriend.h"
@interface SearchByIDViewController ()<AddFriendDelegate>
{
    UIAlertView *loadingAlertView;
}
@end

@implementation SearchByIDViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    //重SIM卡內取得國家 並且顯示國家
    self.TF_Country.text = [TelephonyInfo getCountryCodeString];
    //取得國家代碼 ex:886
    self.ISO_Country_Code =[TelephonyInfo getCountryCodeNumber];
    
    loadingAlertView = [[UIAlertView alloc]initWithTitle:@"搜尋中" message:@"請等待" delegate:nil cancelButtonTitle:nil otherButtonTitles: nil];


}
//選擇國碼
- (IBAction)pushCountrySelectView:(id)sender {
    CountryTableViewController *countryTVC = [self.storyboard instantiateViewControllerWithIdentifier:@"CountryTableViewController"];
    countryTVC.delegate = self;
    [self.navigationController pushViewController:countryTVC animated:YES];
}
- (IBAction)searchButtonEvent:(id)sender {
    //檢查電話有沒有正確
    if (self.TF_account.text.length>0 &&[ValidateText validatePhone:self.TF_account.text])
    {
        //判斷 電話  將國碼與電話合併
        NSString *phone;
        if ([self.TF_account.text hasPrefix:@"0"]) {
            phone = [NSString stringWithFormat:@"%@.%@",self.ISO_Country_Code,[self.TF_account.text substringFromIndex:1]];

        }else
        {
            phone = [NSString stringWithFormat:@"%@.%@",self.ISO_Country_Code,self.TF_account.text];
        }
        //檢查電話是否為自己
        if (![phone isEqualToString:[TelephonyInfo accountAddCountryCode]]) {
            NSDictionary *dict = @{@"phone":phone};
            NSArray *array = @[dict];
            
            [loadingAlertView show];
            
            [FiendFriend sharedInstance].delegate = self;
            //取得輸入的電話人 vcard
            [[FiendFriend sharedInstance]searchFreindForServer:array];
            
            //將鍵盤收下
            [self.TF_account resignFirstResponder];
        }else
        {
            [[[UIAlertView alloc]initWithTitle:@"" message:@"你在找你自己嗎？" delegate:nil cancelButtonTitle:@"ＯＫ" otherButtonTitles: nil]show];
        }
        
    }
}

- (IBAction)TB_againSearchEvent:(id)sender {
    //再次收尋好友時 變換畫面
    [self.TB_StartSearch setHidden:NO];
    [self.LB_NickName setHidden:YES];
    [self.LB_PickerImage setHidden:YES];
    [self.BT_AgainSeach setHidden:YES];
    [self.BT_AddFriend setHidden:YES];
}
- (IBAction)TB_AddFriendEvent:(id)sender {
    
    //增加朋友delegate
    [AddFriend sharedInstance].delegate = self;
    
    //增加朋友
    [[AddFriend sharedInstance]addFreindForJid:self.jidArray];
}
#pragma Tank AddFriendDelegate 增加朋友完畢後的delegate
-(void)AddFriendResult:(id)responseObject
{
    NSLog(@"成功");
}

-(void)errorAddFriend
{
    NSLog(@"失敗");
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma Tank FiendFriendDelegate 取得朋友狀態列表的delegate
-(void)getVcardResult:(id)responseObject
{
    [loadingAlertView dismissWithClickedButtonIndex:0 animated:YES];

    //取得好友之後顯示在畫面上
    NSLog(@"%@",responseObject);
    [self.TB_StartSearch setHidden:YES];
    [self.LB_NickName setHidden:NO];
    [self.LB_PickerImage setHidden:NO];
    [self.BT_AgainSeach setHidden:NO];
    [self.BT_AddFriend setHidden:NO];

    
    [self.LB_NickName setText:responseObject[0][@"NICKNAME"]];
    [self.LB_PickerImage setImage:[UIImage imageNamed:responseObject[0][@"PHOTO_PATH"]]];
    self.jidArray = [NSArray arrayWithObject:responseObject[0][@"jid"]];
}
#pragma Tank FiendFriendDelegate 搜尋失敗
-(void)errorNotFindFriend
{
    NSLog(@"失敗");
    [loadingAlertView dismissWithClickedButtonIndex:0 animated:YES];

}
#pragma mark CountryTableViewControllerDelegate
-(void)selectCountry:(NSDictionary *)countryDict
{
    //選擇完畢後 將國家名稱填上去
    self.TF_Country.text = countryDict[@"country"];
    //選擇完畢後，將國家代碼 帶給 ISO_Country_Code
    self.ISO_Country_Code = countryDict[@"code"];
}


@end
